//페이지 구성완료후 이벤트
$("#list").die();
$("#list").live("pageinit", function() {

	// 페이지 정보
	var page = {
		"dom" : $(this),
		"id" : this.id
	}
	
	var listData = [];


	// 모델
	var model = (function() {
		var my = {};
		
		my.getMonthListData = function (callback) {
			var sql = "select DISTINCT * from (" + 
				"select A.kcal, B.location, C.weight, A.insert_date as idate from (select sum(kcal) as kcal, insert_date from diary_base group by insert_date  ) as A " + 
				"LEFT OUTER join diary_picture as B " + 
				"ON A.insert_date = B.insert_date " + 
				"LEFT OUTER JOIN [diary_weight] as C " + 
				"ON A.[insert_date] = C.insert_date " + 
				"UNION ALL " + 
				"select A.kcal, B.location, C.weight, B.insert_date as idate from diary_picture as B " + 
				"LEFT OUTER join (select sum(kcal) as kcal, insert_date from diary_base group by insert_date  ) as A " + 
				"ON B.insert_date = A.insert_date " + 
				"LEFT OUTER JOIN [diary_weight] as C " + 
				"ON B.insert_date = C.insert_date " + 
				"UNION ALL " + 
				"select A.kcal, B.location, C.weight, C.insert_date as idate from diary_weight as C " + 
				"LEFT OUTER join (select sum(kcal) as kcal, insert_date from diary_base group by insert_date  ) as A " + 
				"ON C.insert_date = A.insert_date " + 
				"LEFT OUTER JOIN [diary_picture] as B " + 
				"ON C.insert_date = B.insert_date " + 
				") where idate BETWEEN '" + UTIL.firstDate(UTIL.today()) + "' and '" + UTIL.lastDate(UTIL.today()) + "' ORDER BY idate";
			Storage.execDataBase(sql, callback);
		}
		return my;
	})();

	// 뷰
	var view = (function() {
		
		var my = {};
		
		my.getMoveBackButton = function () {
			return $("#move_back_button", page.dom);
		};
		
		my.getListList = function () {
			return $(".list_list", page.dom)
		}
		
		my.getListListContent = function () {
			return $(".list_list_content", page.dom)
		}
		
		my.getListListTemplate = function () {
			return $(".list_list_template", page.dom)
		}
		
		return my;
	})();

	// 컨트롤러
	var controll = (function() {
		var setListList = function () {
			// 음식 리스트 세팅
			view.getListListContent().onList("makeList", {
				'content' : view.getListListContent(),
				'template' : view.getListListTemplate(),
				'target' : function($item) {
					return {
						"LIST_IMAGE" : $(".list_image", $item),
						"TITLE" : $(".title", $item),
						"CONTENT" : $(".content", $item),
						"SUB_TITLE" : $(".sub_title", $item)
					};
				},
				'data' : function($item, data) {
					if(data.location){
						$item.LIST_IMAGE.removeClass("list_image");
						$item.LIST_IMAGE.html("<img src='" + data.location + "' width='100' height='100'/>");
					}
					
					var infoData = Storage.getLocalData("myInfoData");
					
					if(data.weight){
						var kcal = (data.weight * 24) + ((data.weight * 24) * 0.3);
						var target_kcal = Math.round((kcal - ((data.weight - infoData.target_weight) * 7200)/120) * 10) / 10;
						$item.SUB_TITLE.text( target_kcal + "kcal / " + (data.kcal || "0") + "kcal");
					}
					else {
						$item.SUB_TITLE.text( "입력 값이 부족합니다. /" + (data.kcal || "0") + "kcal");
					}
					$item.TITLE.text(UTIL.dateFormat(data.idate));
					$item.CONTENT.text( (data.weight || "0") + "kg");
				},
				'onAddsFinish' : function() {
				}
			});
		};
		
		var my = {};

		var getMonthListData = function () {
			model.getMonthListData(function (result) {
				listData = result;
				view.getListListContent().onList("setList", result);
			});
		};
		// controll 초기화
		my.init = function() {
			setListList();
			getMonthListData();
			view.getMoveBackButton().on("atap", function () {
				history.back();
			});
			
			view.getListList().die();
			view.getListList().live("atap", function () {
				var index = view.getListList().index($(this));
				var param = {
					thisDate : listData[index].idate
				}
				
				$.mobile.moveBack(param);
			});
		}

		
		return my;
	})();

	// controll 초기화 실행


	// 페이지 시작시
	page.dom.on("startPage", function(e, data, prevPageId) {
		controll.init();
	});
	// 페이지 재시작시(뒤로 오는 경우)
	page.dom.on("reStartPage", function() {
	})
	
});